﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.ComponentModel;
using System.Web.Configuration;

namespace HospitalSystem.Core
{
    public class findchildLog
    {
        public void BindData(String searchchild,GridView listchild)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);

            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT CID,FirstName,LastName FROM Person where CID=@text or FirstName=@text or Lastname=@text", scon);
            //SqlCommand cmd = new SqlCommand("SELECT t2.CaseNo as CaseNo,t2.CID as CID,t3.FirstName as FirstName,t3.LastName as LastName FROM (SELECT cid,max(CaseNo) as caseno FROM [Evaluation] group by CID) t2 join Person t3 on t2.CID=t3.CID order by t3.FirstName where t2.CID=@text or t3.FirstName=@text or t3.Lastname=@text", scon);
            cmd.Parameters.AddWithValue("@text", searchchild);
            SqlDataReader reader = cmd.ExecuteReader();


            listchild.DataSource = reader;
            listchild.DataBind();
            scon.Close();
        }


        public void BindDatadek(Label SelectStat, GridView listchild,Label fname,Label lname)
        {
            SelectStat.Text = string.Empty;
            bool selectorChecked3 = false;

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("CID");
            dt2.Columns.Add("FirstName");
            dt2.Columns.Add("LastName");



           
            foreach (GridViewRow row in listchild.Rows)
            {
                RadioButton cb = (RadioButton)row.FindControl("CaseSelector3");
                if (cb != null && cb.Checked)
                {
                    selectorChecked3 = true;

                    DataRow dr2;
                    dr2 = dt2.NewRow();
                    string CID = listchild.Rows[row.RowIndex].Cells[0].Text;
                    string FirstName = listchild.Rows[row.RowIndex].Cells[1].Text;
                    string LastName = listchild.Rows[row.RowIndex].Cells[2].Text;
                    SelectStat.Text += string.Format(CID);
                    fname.Text = string.Format(FirstName);
                    lname.Text = string.Format(LastName);
                    dt2.Rows.Add(CID, FirstName, LastName);


                }
            }
            SelectStat.Visible = selectorChecked3;


        }





        //สร้าง case จาก case มากสุด
        public void Bindcase(Label SelectStat,Label caseno)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);

            SqlCommand cmd2 = new SqlCommand("SELECT [CID],Max(CaseNo) as caseno FROM [Evaluation] where CID=@CID group by CID", scon);


            scon.Open();
            cmd2.Parameters.AddWithValue("@CID", SelectStat.Text);

            SqlDataReader reader2 = cmd2.ExecuteReader();



            if (reader2.Read())
            {

                caseno.Text = reader2["caseno"].ToString();

            }
            else
            {
                //MessageBox("ไม่พบเลขบัตรครับ");
            }
            scon.Close();
            reader2.Close();


        }


        public void creDetails(Label SelectStat, Label caseno, Label psnumber)
        
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand command22 = new SqlCommand("INSERT INTO [ServiceProvisionVsPerson] (CID,CaseNo,PSNumber) VALUES(@CID,@CaseNo,@PSNumber)", scon);
            scon.Open();

            command22.Parameters.AddWithValue("@CID", SelectStat.Text);
            command22.Parameters.AddWithValue("@CaseNo", caseno.Text);
            command22.Parameters.AddWithValue("@PSNumber", psnumber.Text);


            command22.ExecuteNonQuery();
            scon.Close();
        }








        //public void showcaseall(Label cid_label, GridView casenoshow)
        //{
        //    string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
        //    SqlConnection scon = new SqlConnection(constr);
        //    SqlCommand command22 = new SqlCommand("SELECT [CID],[CaseNo],[EvalDateTime] FROM [Evaluation] where CID='1669800114214'", scon);
        //    scon.Open();

        //    command22.Parameters.AddWithValue("@CID", SelectStat.Text);
        //    command22.Parameters.AddWithValue("@CaseNo", caseno.Text);
        //    command22.Parameters.AddWithValue("@PSNumber", psnumber.Text);


        //    command22.ExecuteNonQuery();
        //    scon.Close();
        //}




    }
}